﻿using System;
using System.Collections.Generic;
using System.IO;
using eDataBoard.basePage.log;
using eDataBoard.basePage;
using System.Linq;

namespace OpcSelfHost.WebApi.common.Impl
{
    class FileCommonImpl : IFileCommon
    {
        /*
         添加文件
         */
        public void addFile(string path, string val)
        {
            try
            {
                if (!File.Exists(path))
                {
                    FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);

                    StreamWriter sw = new StreamWriter(fs);
                    sw.Write(val);
                    sw.Flush();
                    sw.Close();
                }
                else
                {
                    FileStream fs = new FileStream(path, FileMode.Append);
                    //文本写入
                    StreamWriter sw = new StreamWriter(fs);
                    sw.Write(val);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception e)
            {
                Log.LogWrite(e.Message,"error");
            }
        }

        /// <summary>
        /// 覆盖形式生成文件
        /// </summary>
        /// <param name="path">完整地址</param>
        /// <param name="val">插入内容</param>
        public void createFile(string path, string val)
        {
            try
            { 
                    FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.Write(val);
                    sw.Flush();
                    sw.Close();
                
            }
            catch (Exception e)
            {
                Log.LogWrite(e.Message,"error");
            }
        }


       

        /// <summary>
        /// 读取文件夹所有文件名
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public List<string> readDirFiles(string path)
        {
            DirectoryInfo folder = new DirectoryInfo(path);
            List<String> list = new List<string>();

            FileInfo[] files = folder.GetFiles();
             

            list = files.OrderBy(s => s.LastWriteTime)
                .ThenBy(s => s.FullName)
                .Select(s => s.FullName).ToList();

            return list;
        }

         

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string readFile(string path)
        {
            try
            {
                String readTxt = "";
                readTxt = File.ReadAllText(path);
                //StreamReader srReadFile = new StreamReader(path);
                //while (!srReadFile.EndOfStream)
                //{
                //    string strReadLine = srReadFile.ReadLine(); //读取每行数据
                //    readTxt += strReadLine;
                //}
                //// 关闭读取流文件
                //srReadFile.Close();
                //Console.ReadKey();
                return readTxt;
            }
            catch (Exception e)
            {
                Log.LogWrite(e.Message,"error");
                return "error";
            }
        }


 
            
        public bool delFile(string path)
        {

            try
            {
              
               File.Delete(path);
               
                return true;
            }
            catch (Exception e)
            {
                Log.LogWrite(e.Message, "error");
                return false;
            }

        }

 



    }
}
